package com.hnxxxy.sysmgr.controller;

import com.github.pagehelper.PageHelper;
import com.hnxxxy.common.AjaxRtnEntity.AjaxRtnEntity;
import com.hnxxxy.common.annotation.LogAnno;
import com.hnxxxy.common.tools.Help;
import com.hnxxxy.common.tools.TableObj;
import com.hnxxxy.common.tools.TreeObj;
import com.hnxxxy.sysmgr.common.Constant;

import com.hnxxxy.sysmgr.pojo.Menu;
import com.hnxxxy.sysmgr.service.MenuService;
import com.hnxxxy.sysmgr.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Json
 * @date 2020-26-8:16
 */
@RestController
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @RequestMapping("loadAllMenu")
    @LogAnno(description = "查询所有菜单")
    public TableObj queryAllStaffForList(TableObj obj, MenuVo menuVo){
        PageHelper.startPage(obj.getCurr(), obj.getNums());
        List<Menu> list = menuService.queryAllMenu(menuVo);
        return Help.getPageTableRtn(obj,list);
    }
    /**
     * 加载菜单管理添加的下拉树
     */
    @RequestMapping("loadMenuManagerLeftTreeJson")
    @LogAnno(description = "加载菜单管理添加的下拉树")
    public TableObj loadMenuManagerLeftTreeJson(MenuVo menuVo){
        menuVo.setType(Constant.TYPE_MENU);
        List<Menu> list=this.menuService.queryAllMenuForList(menuVo);
        List<TreeObj> nodes= new ArrayList<>();
        //把list里面的数据放到nodes
        for (Menu menu : list) {
            Integer id=menu.getId();
            Integer pid;
            if (menu.getParentid() == null){
                 pid = 0;
            }else {
                 pid=menu.getParentid();
            }
            String title=menu.getMenuname();
            nodes.add(new TreeObj(id, pid, title));
        }
        return new TableObj(nodes);
    }
    /**
     * 添加菜单
     */
    @RequestMapping("addMenu")
    @LogAnno(description = "添加菜单",descriptiontype = "addMenu")
    public AjaxRtnEntity addMenu(MenuVo menuVo) {

            menuVo.setType(Constant.TYPE_MENU);
            AjaxRtnEntity ajaxRtnEntity = new AjaxRtnEntity(this.menuService.addMenu(menuVo),"添加成功！",null);
            return ajaxRtnEntity;

    }
    /**
     * 修改菜单
     */
    @RequestMapping("updateMenu")
    @LogAnno(description = "更新菜单",descriptiontype = "updateMenu")
    public AjaxRtnEntity updateMenu(MenuVo menuVo) {

        return new AjaxRtnEntity(this.menuService.updateMenu(menuVo),"更新成功！",null);

    }

    /**
     * 删除菜单
     */
    @RequestMapping("deleteMenu")
    @LogAnno(description = "删除菜单",descriptiontype = "deleteMenu")
    public AjaxRtnEntity deleteMenu(Integer id) {
            return new AjaxRtnEntity(this.menuService.deleteMenu(id),"删除成功！",null);
    }

    /**
     * 查询当前的ID的菜单有没有子菜单
     */
    @RequestMapping("checkMenuHasChildrenNode")
    @LogAnno(description = "判断是否有子菜单")
    public Map<String, Object> checkMenuHasChildrenNode(MenuVo menuVo) {
        Map<String, Object> map = new HashMap<String, Object>();

        Integer count=this.menuService.queryMenuByPid(menuVo.getId());
        if (count > 0) {
            map.put("value", true);
        } else {
            map.put("value", false);
        }
        return map;
    }

    /**
     * 批量删除菜单
     */
    @RequestMapping("batchDeletemenu")
    @LogAnno(description = "批量删除菜单",descriptiontype = "batchDeletemenu")
    public AjaxRtnEntity deleteBatchMenus(MenuVo menuVo) {
        return new AjaxRtnEntity(this.menuService.batchDeletemenu(menuVo.getIds()),"删除成功！",null);
    }
}
